package com.vv.edu.info.manager.service;

import com.vv.edu.info.manager.dao.BaseStudentDao;
import com.vv.edu.info.manager.domain.StudentDomain;
import com.vv.edu.info.manager.factory.StudentDaoFactory;

public class StudentService {

    // 通过学生库管的工厂类，获取库管对象。
    private final BaseStudentDao studentDao = StudentDaoFactory.getStudentDao(); // 创建库管对象

    public boolean addStudent(StudentDomain stu) {
        return studentDao.addStuent(stu); // 将学生对象传递给StudentDao库管中的addStudent方法
    }

    public boolean isExists(String id) {
        StudentDomain[] stus = studentDao.findAllStudent();
        // 假设id在数组中不存在
        boolean exists = false;
        // 遍历数组，获取每一个学生对象，准备判断
        for (StudentDomain studentDomain : stus) {
            if (studentDomain != null && studentDomain.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    public StudentDomain[] findAllStudent() {
        // 1.调用库管对象的findAllStudent获取学生对象数组
        StudentDomain[] allStudent = studentDao.findAllStudent();
        // 2.判断数组中是否有学生信息(有：返回地址，没有返回null)
        boolean flag = false;
        for (StudentDomain stu : allStudent) {
            if (stu != null) {
                flag = true;
                break;
            }
        }
        if(flag) return allStudent;
        else return null;
    }

    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }

    public void updataStudent(String updataId, StudentDomain newStu) {
        studentDao.updataStudent(updataId, newStu);
    }
}
